﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
namespace lcs.Web
{
    /// <summary>
    /// HandlerMbm 的摘要说明
    /// </summary>
    public class HandlerMbm : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string keyword = context.Request.QueryString["keyword"];
            string type = context.Request.QueryString["key1"];
            string key = context.Request.QueryString["key"];
            string keyb = context.Request.QueryString["keyb"];
            string keym = context.Request.QueryString["keym"];
            string keys = context.Request.QueryString["keys"];
            string keyxy = context.Request.QueryString["keyxy"];
            if (keyword != null)
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                // 通过JavaScriptSerializer对象的Serialize序列化为["value1","value2",...]的字符串    
                string jsonString = serializer.Serialize(GetFilteredList(keyword));
                context.Response.Write(jsonString); // 返回客户端json格式数据  
            }
            if (key != null&& type!=null)
            {
                context.Response.Write(GetLikeTitle(System.Web.HttpUtility.UrlDecode(context.Request["key"]), System.Web.HttpUtility.UrlDecode(context.Request["key1"])));
                context.Response.Charset = "gb2312";
                //context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                context.Response.ContentType = "application/json";
                context.Response.End(); 
            }
            if (keyb != null )
            {
                context.Response.Write(GetLikeJobTitle(System.Web.HttpUtility.UrlDecode(context.Request["keyb"])));
                context.Response.Charset = "gb2312";
                //context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                context.Response.ContentType = "application/json";
                context.Response.End();
            }
            if (keym != null)
            {
                context.Response.Write(GetLikexy(System.Web.HttpUtility.UrlDecode(context.Request["keyxy"]),keym,keys));
                context.Response.Charset = "gb2312";
                //context.Response.ContentEncoding = System.Text.Encoding.UTF8;
                context.Response.ContentType = "application/json";
                context.Response.End();

            }
        }
        public String GetLikexy(String keyword,string mtid,string sid)
        {

            if (keyword != "")
            {
                String strJson = String.Empty;
                lcs.BLL.protocolinfo bll = new BLL.protocolinfo();
                DataSet ds = bll.GetList(" and  mms like '%" + keyword + "%' and mtid="+mtid+" and sid="+sid);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[0]);
                    strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[0]);
                }
                return strJson;
            }
            else
                return "";
        }
        public String GetLikeJobTitle(String keyword)
        {
            
            if (keyword != "")
            {
                String strJson = String.Empty;
                lcs.BLL.materialbasicinfo bll = new BLL.materialbasicinfo();
                DataSet ds = bll.GetList(" and  mms like '%" + keyword + "%'");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[0]);
                    strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[0]);
                }
                return strJson;
            }
            else
                return "";
        }
        public String GetLikeTitle(String keyword,string type)
        {
            String strJson = String.Empty;
            DataSet ds = null;
            if (type == "0")//投标物料
            {
                lcs.BLL.materialbasicinfo mb = new BLL.materialbasicinfo();
                ds = mb.GetList(" and  mms like '%" + keyword + "%' and mbm not in (select mbm from bidmaterialinfo)");
            }
            else
            {
                lcs.BLL.bidmaterialinfo mb = new BLL.bidmaterialinfo();
                ds = mb.GetList(" and  mms like '%" + keyword + "%' and mtype='"+type+"'");
            }
            if (ds.Tables[0].Rows.Count > 0)
            {                
                strJson = Newtonsoft.Json.JsonConvert.SerializeObject(ds.Tables[0]);
            }
            return strJson;
        }
        private string[] GetFilteredList(string keyword)
        {
            lcs.BLL.materialbasicinfo bll = new lcs.BLL.materialbasicinfo();
            DataSet ds = bll.GetList(" and mbm like '%" + keyword + "%'");

            List<string> proList = new List<string>();

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //此处选项一般从数据库中读取，然后循环添加给countrylist对象即可。
                proList.Add(ds.Tables[0].Rows[i]["mbm"].ToString());
            }

            return proList.ToArray();
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}